package com.sixmac.police.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.constant.Constant;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Md;
import com.sixmac.core.shiro.ShiroKit;
import com.sixmac.core.toolbox.Paras;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.module.*;
import com.sixmac.police.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/manage")
public class AdminManageController extends BaseController {
    private static String CODE = "admin";
    private static String PREFIX = "t_admin_user";
    private static String LIST_SOURCE = "adminUser2.list";
    private static String BASE_PATH = "/police/";

    @Autowired
    AdminUserService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
        if(user == null){
            return "/police/default.html";
        }
        String number = getParameter("number");
        String phone = getParameter("phone");
        Integer roleId = getParameterToInt("roleId",null);
        Paras paras = Paras.create().set("pId",user.getId()).set("number",number).set("phone",phone).set("roleId",roleId);
        Object grid = paginate(Md.getSql(LIST_SOURCE,paras));
        mm.put("role",Blade.create(AdminRole.class).findAll());
        mm.put("grid",grid);
        return BASE_PATH + "manager_list.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("role",Blade.create(AdminRole.class).findAll());
        AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
        if (user.getCityId() != null){
            mm.put("cityCode", user.getCityId());
        }else{
            mm.put("areaCode", user.getAreaId());
        }
        mm.put("code", CODE);
        return BASE_PATH + "add_manager.html";
    }

    @RequestMapping(KEY_EDIT)
    public String edit(ModelMap mm) {
        mm.put("user",Blade.create(AdminUser.class).findById(getParameterToInt("id")));
        AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
        if (user.getCityId() != null){
            mm.put("cityCode", user.getCityId());
        }else{
            mm.put("areaCode", user.getAreaId());
        }
        mm.put("role",Blade.create(AdminRole.class).findAll());
        mm.put("code", CODE);
        return BASE_PATH + "add_manager.html";
    }

    @RequestMapping(KEY_REMOVE)
    public String remove(@RequestParam String id,ModelMap mm) {
        service.deleteByIds(id);
        AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
        if(user == null){
            return "/police/default.html";
        }
        Paras paras = Paras.create().set("pId",user.getId());
        Object grid = paginate(Md.getSql(LIST_SOURCE,paras));
        mm.put("grid",grid);
        return BASE_PATH + "manager_list.html";
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
        if(user == null){
            return error(SAVE_FAIL_MSG);
        }
        Integer areaId = getParameterToInt("area");
        String number = getParameter("number");
        String name = getParameter("name");
        String password = getParameter("password");
        String phone = getParameter("phone");
        Integer roleId = getParameterToInt("roleId");
        Integer id = getParameterToInt("id",0);
        AdminUser adminUser = new AdminUser();
        adminUser.setCreateDate(new Date());
        adminUser.setRoleId(roleId);
        adminUser.setName(name);
        adminUser.setPhone(phone);
        adminUser.setPassword(ShiroKit.md5(password, ""));
        adminUser.setNumber(number);
        adminUser.setAreaId(areaId);
        adminUser.setpId(user.getId());
        boolean temp  = true;
        if (id!=0){
            adminUser.setId(id);
            temp = Blade.create(AdminUser.class).update(adminUser);
        }else{
            Blade.create(AdminUser.class).save(adminUser);
        }
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping("/findProvince")
    public List<Province> findProvince() {
        Blade provinceBlade = Blade.create(Province.class);
        return provinceBlade.findAll();
    }

    @ResponseBody
    @RequestMapping("/findCity")
    public List<City> findCity(String provinceCode) {
        Blade cityBlade = Blade.create(City.class);
        return cityBlade.findBy("provinceCode = #{provinceCode} ", Paras.create().set("provinceCode", provinceCode));
    }

    @ResponseBody
    @RequestMapping("/findArea")
    public List<Area> findArea(String cityCode,String areaCode) {
        Blade areaBlade = Blade.create(Area.class);
        List< Area> area = null;
        if (cityCode!=null&&cityCode.trim().length()>0){
            area  =  areaBlade.findBy("cityCode = #{cityCode} ", Paras.create().set("cityCode", cityCode));
        }else{
            area  =  areaBlade.findBy("areaCode = #{areaCode} ", Paras.create().set("areaCode", areaCode));
        }
        return area;
    }
}
